package com.algo.Al.controller;

import com.algo.Al.common.R;
import com.algo.Al.mapper.BdProoncegasMapper;
import com.algo.Al.mapper.BdProoncegasdetailMapper;
import com.algo.Al.pojo.*;
import com.algo.Al.service.BdCompanyinfoService;
import com.algo.Al.service.DsIndusbottleService;
import com.algo.Al.service.PmBottleproductiondetailService;
import com.algo.Al.service.impl.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Author: guoxing
 * @Email: gx_study2022@163.com
 * @Description:
 * @Date: 2024/6/3 16:11
 * @ClassName: T
 **/
@Log4j
@RestController
@RequestMapping(value = "/t")
public class T {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private DsShujuServiceImpl dsShujuService;
    @Autowired
    private DsIndusbottleServiceImpl dsIndusbottleService;
//    @GetMapping("/t1")
    public R<DsShuju> getUserById() {
        int id=1;
        for (int i = 1; i <43308;i++ ) {
            DsShuju ds=dsShujuService.getById(i);
            DsIndusbottle db=new DsIndusbottle();
            db.setId(ds.getId());
            db.setProperty(ds.getProperty());
            db.setSigncode(ds.getSigncode());
            db.setDevicetype(ds.getDevicetype());
            db.setUnitcode(ds.getUnitcode());
            db.setBottlecode(ds.getBottlecode());
            db.setBarcode(ds.getBarcode());
            db.setBottlesupplierid(ds.getBottlesupplierid());
            db.setBottlesuppliername(ds.getBottlesuppliername());
            db.setProductiondate(ds.getProductiondate());
            db.setLastcheckdate(ds.getLastcheckdate());
            db.setNextcheckdate(ds.getNextcheckdate());
            db.setBottlemedium(ds.getBottlemedium());
            db.setBottlevolume(ds.getBottlevolume());
            db.setNominalpressure(ds.getNominalpressure());
            db.setDisable(ds.getDisable());
            dsIndusbottleService.save(db);
        }
        DsShuju ds=dsShujuService.getById(1);

        if (ds == null) {
            return R.error("查询无果");
        }
        return R.success(ds);
    }



    @Autowired
    private BdProoncegasdetailServiceImpl bdProoncegasdetailService;
    @Resource
    private BdProoncegasdetailMapper bdProoncegasdetailMapper;

    @Autowired
    private BdProoncegasServiceImpl dbProoncegasService;

    @Resource
    private BdProoncegasMapper bdProoncegasMapper;



    //DB老表总条数
    private static int DBLIST=138;


    //SMINFO表有UUID的那条ID
    private static int UUIDINDEX=203;
    //SMINFO表总条数ID
    private static int SMINFO=339;

    @Autowired
    private SmProductininfodetailServiceImpl smProductininfodetailService;
    @Autowired
    private SmProductininfoServiceImpl smProductininfoServiceImpl;

    //添加UUID只需要添加进两个BD表中
    //先添加进去list表
    /**
     * @Description 给BDInfo表增加UUID
     * @author guoxing
     * @date 2024/6/13 22:00
     * @return R<BdProoncegas>
     */
    @GetMapping("/addUUIdToList")
    public R<BdProoncegas> addUUIdToList(){
        int id=1025;
        BdProoncegas b1 = null;
        for (int i = 1; i <DBLIST; i++){
            b1=dbProoncegasService.getById(i);
            b1.setUuid(id);
            dbProoncegasService.updateById(b1);

            id+=1;
        }
        return R.success(b1);
    }


    /**
     * @Description 给BDDetail表增加UUID
     * @author guoxing
     * @date 2024/6/13 22:01
     * @return R<BdProoncegasdetail>
     */
    @GetMapping("/addUUIdToDetial")
    public R<BdProoncegasdetail> addUUIdToDetial(){
        BdProoncegasdetail b1=null;
        BdProoncegas b2 = null;
        List<BdProoncegasdetail> bdProoncegasdetail=bdProoncegasdetailMapper.selectList(null);
        for (int i = 1; i <bdProoncegasdetail.size() ;i++ ) {
            b1=bdProoncegasdetail.get(i);
            for (int j = 1; j <DBLIST; j++){
                b2=dbProoncegasService.getById(j);
                if (b1.getMainid().equals(b2.getId())){
                    b1.setUuid(b2.getUuid());
                    bdProoncegasdetailService.updateById(b1);
                }
            }
        }
        return R.success(b1);
    }

    /**
     * @Description DBInfo数据加进去SMInfo表
     * @author guoxing
     * @date 2024/6/13 22:01
     * @return R<SmProductininfo>
     */
    @GetMapping("/dbGasToSmInfo")
    public R<SmProductininfo> dbGasToSmInfo(){
        SmProductininfo smInfo = new SmProductininfo();
        for (int i = 1; i <DBLIST; i++) {
            BdProoncegas gas=dbProoncegasService.getById(i);
            smInfo.setTotalid(41);
            smInfo.setStoragenum("CR00000040");
            smInfo.setProductname(gas.getProductname());
            smInfo.setGuige(gas.getVolume());
            smInfo.setTechparams(gas.getCustechparams());
            smInfo.setBottlebarcode(gas.getBottlebarcode());
            smInfo.setBottlecode(gas.getBottlecode());
            smInfo.setOneornormcode(gas.getProductcode());
            smInfo.setUuid(gas.getUuid());
            smInfo.setStoragebinid(2255);
            smInfo.setStoragebin("A-1-A1");
            smInfo.setWarehouseid(27);
            smInfo.setWarehouse("一次气库");
            smProductininfoServiceImpl.save(smInfo);
        }
        return R.success(smInfo);
    }


    /**
     * @Description 根据SMInfo表中的UUID更改BDDetail表中的MainID
     * @author guoxing
     * @date 2024/6/13 22:02
     * @return R<SmProductininfo>
     */
    @GetMapping("/updateID")
    public R<SmProductininfo> updateID(){
        List<BdProoncegasdetail> bdProoncegasdetail=bdProoncegasdetailMapper.selectList(null);
        SmProductininfo smin=new SmProductininfo();
        for (int i = UUIDINDEX; i < SMINFO; i++){
            smin=smProductininfoServiceImpl.getById(i);
            for (int j=0;j<bdProoncegasdetail.size(); j++) {
                if (bdProoncegasdetail.get(j).getUuid().equals(smin.getUuid())){
                    bdProoncegasdetail.get(j).setMainid(smin.getId());
                    bdProoncegasdetailService.updateById(bdProoncegasdetail.get(j));
                }
            }
        }
        return R.success(smin);
    }

    /**
     * @Description 将BDDetail表中的数据加入SMDetail中
     * @author guoxing
     * @date 2024/6/13 22:03
     * @return R<SmProductininfodetail>
     */
    @GetMapping("/dbDetailToSmDetail")
    public R<SmProductininfodetail> dbDetailToSmDetail(){
        SmProductininfodetail sd=new SmProductininfodetail();
        List<BdProoncegasdetail> bdProoncegasdetail=bdProoncegasdetailMapper.selectList(null);
        for (int i = 0; i < bdProoncegasdetail.size(); i++){
            BdProoncegasdetail b=bdProoncegasdetail.get(i);
            sd.setMainid(b.getMainid());
            sd.setGasid(b.getGasid());
            sd.setConcentration(b.getConcen());
            sd.setOneconcentration(b.getOneconcentration());
            sd.setIsbottom(b.getIsbottomgas());
            sd.setAnalysemethod(b.getAnalysemethodname());
            sd.setOnebottlecode(b.getOnebottlecode());
            sd.setCustomerconcentration(b.getCusconcern());
            smProductininfodetailService.save(sd);
        }
        return R.success(sd);
    }

    @Autowired
    private PmBottleproductionServiceImpl pmBottleproductionService;
    @Autowired
    private PmBottleproductiondetailServiceImpl pmBottleproductiondetailService;






    //DB老表总条数
    private static int DBLIST1=138;


    //PMINFO表有UUID的那条ID
    private static int UUIDINDEX1=195;
    //PMINFO表总条数ID
    private static int SMINFO1=331;
    @GetMapping("/dbGasToPMInfo")
    public R<PmBottleproduction> dbGasToPMInfo(){
        PmBottleproduction pmInfo = new PmBottleproduction();
        for (int i = 1; i <DBLIST1; i++) {
            BdProoncegas gas=dbProoncegasService.getById(i);
            pmInfo.setNum(1);
            pmInfo.setGbname(gas.getProductname());
            pmInfo.setTechparams(gas.getCustechparams());
            pmInfo.setBottlebarcode(gas.getBottlebarcode());
            pmInfo.setBottlevolume2(gas.getVolume());
            pmInfo.setOneornormcode(gas.getProductcode());
            pmBottleproductionService.save(pmInfo);
        }
        return R.success(pmInfo);
    }



    @GetMapping("/updatePMID")
    public R<PmBottleproduction> updatePMID(){
        List<BdProoncegasdetail> bdProoncegasdetail=bdProoncegasdetailMapper.selectList(null);
        PmBottleproduction pmin=new PmBottleproduction();
        for (int i = UUIDINDEX1; i < SMINFO1; i++){
            pmin=pmBottleproductionService.getById(i);
            for (int j=0;j<bdProoncegasdetail.size(); j++) {
                if (bdProoncegasdetail.get(j).getUuid().equals(pmin.getUuid())){
                    bdProoncegasdetail.get(j).setMainid(pmin.getId());
                    bdProoncegasdetailService.updateById(bdProoncegasdetail.get(j));
                }
            }
        }
        return R.success(pmin);
    }


    @GetMapping("/dbDetailToPmDetail")
    public R<PmBottleproductiondetail> dbDetailToPmDetail(){
        PmBottleproductiondetail pd=new PmBottleproductiondetail();
        List<BdProoncegasdetail> bdProoncegasdetail=bdProoncegasdetailMapper.selectList(null);
        for (int i = 0; i < bdProoncegasdetail.size(); i++){
            BdProoncegasdetail b=bdProoncegasdetail.get(i);
            pd.setMainid(b.getMainid());
            pd.setGasid(b.getGasid());
            pd.setConcentration(b.getConcen());
            pd.setOneconcentration(b.getOneconcentration());
            pd.setIsbottom(b.getIsbottomgas());
            pd.setAnalysemethod(b.getAnalysemethodname());
            pd.setGasweight(BigDecimal.valueOf(b.getGasweight()));
            pd.setOnebottlecode(b.getOnebottlecode());
            pd.setCustomerconcentration(b.getCusconcern());
            pmBottleproductiondetailService.save(pd);
        }
        return R.success(pd);
    }

    //修改为全0
    @GetMapping("/changeState1")
    public DsIndusbottle changeState1(){
        DsIndusbottle d=null;
        for (int i=1;i<43322;i++){
             d=dsIndusbottleService.getById(i);
            if (d != null) {
                d.setState("0");
                dsIndusbottleService.updateById(d);
            }
        }

       return d;
    }


    @Autowired
    private BdCompanyinfoServiceImpl bdCompanyinfoService;
    @Autowired
    private BdCeshiServiceImpl bdCeshiService;

    @GetMapping("/changeState")
    private BdCompanyinfo changeState(){
        String ceshi="迈斯特(河北)检测集团有限公司";
        //查询BDCeshi数据库的总数据数
        int ceshiSize=bdCeshiService.count();
        //循环读取A数据库中的客户名称
        int id=562;
        BdCeshi c=null;
        for (int i = 1; i <ceshiSize; i++){
            c=bdCeshiService.getById(i);
            //将A数据库中的客户名称赋值给B数据库
            String comname=c.getComname();
            BdCompanyinfo b = bdCompanyinfoService.getOne(new QueryWrapper<BdCompanyinfo>()
                    .eq("ComName", comname).orderByAsc("id") // 假设id是主键或者一个可以用来排序的唯一字段
                    .last("OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY"));
            if (b != null) {
                b.setComaddress(c.getComaddress());
                b.setPerson(c.getPerson());
                b.setMobilephone(c.getMobilephone());
                b.setTaxpayerid(c.getTaxpayerid());
                b.setBank(c.getBank());
                b.setBankaccount(c.getBankaccount());
                b.setComnamekp(c.getComnamekp());
                b.setAddresskp(c.getComaddress());
                b.setPhonekp(c.getPhonekp());
                b.setKptype(c.getKptype());
                b.setProvinceid(c.getProvinceid());
                b.setCityid(c.getCityid());
                b.setAddname(c.getAddname());
                b.setPaytype(c.getPaytype());
                bdCompanyinfoService.updateById(b);
            }else {
                b=new BdCompanyinfo();
                b.setId(id);
                b.setComname(c.getComname());
                b.setSimplifiedcode(c.getSimplifiedcode());
                b.setComaddress(c.getComaddress());
                b.setPerson(c.getPerson());
                b.setMobilephone(c.getMobilephone());
                b.setTaxpayerid(c.getTaxpayerid());
                b.setBank(c.getBank());
                b.setBankaccount(c.getBankaccount());
                b.setComnamekp(c.getComnamekp());
                b.setAddresskp(c.getComaddress());
                b.setPhonekp(c.getPhonekp());
                b.setKptype(c.getKptype());
                b.setProvinceid(c.getProvinceid());
                b.setCityid(c.getCityid());
                b.setAddname(c.getAddname());
                b.setPaytype(c.getPaytype());
                bdCompanyinfoService.save(b);
                id+=1;
            }
        }
        return bdCompanyinfoService.getById(11);
    }


    @Autowired
    private Sheet50701ServiceImpl s;
    @GetMapping("/updateSheet50701")
    public Sheet50701 updateSheet50701(){
        //循环读取sheet0701表
        Sheet50701 s1 = null;
        for (int i = 1;i <=217;i++) {
            //找出表中simplifiedcode信息
            s1=s.getById(i);
            //查询BdCompanyinfo表中simplifiedcode等于Sheet50701表中simplifiedcode的第一条数据
            BdCompanyinfo b=bdCompanyinfoService.getOne(new QueryWrapper<BdCompanyinfo>()
                    .eq("SimplifiedCode", s1.getSimplifiedcode()).orderByAsc("id") // 假设id是主键或者一个可以用来排序的唯一字段
                    .last("OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY"));
            //将Sheet50701表中的TaxpayerID和BankAccount赋值给BdCompanyinfo的这条数据并保存
            if (b != null){
                b.setTaxpayerid(s1.getTaxpayerid());
                b.setBankaccount(s1.getBankaccount());
                bdCompanyinfoService.updateById(b);
                log.info("更新了一条数据，数据的ID是"+b.getId()+"使用数据ID为"+s1.getId()+"公司简称是"+s1.getSimplifiedcode());
            }
        }
        return s1;
    }




}
